/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.etop.weibo.util;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jansing
 * lz_jansing@163.com
 */
public class DaoProperties {
    private static Properties prop = loadProperties();
    private static Map<String, Object> daoMap = new HashMap<>();
    
    private static Properties loadProperties(){
        try {
            Properties p = new Properties();
            p.load(new BufferedInputStream(DaoProperties.class.getResourceAsStream("dao.properties")));
            return p;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DaoProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DaoProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static final Object getDao(String daoName){
        try {
            if(daoMap.containsKey(daoName.toUpperCase())){
                return daoMap.get(daoName.toUpperCase());
            }
            Class daoClass = Class.forName(prop.getProperty(daoName));
            Object dao = daoClass.newInstance();
            daoMap.put(daoName.toUpperCase(), dao);
            return dao;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DaoProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DaoProperties.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DaoProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
